Flags And Toggles
Flag variables & Toggles The term "flag variable", or simply "flag" refers not to a particular type of variable, but to a specific use of one, whereupon it is assigned either of any two values, which may then be used to represent yes / no, true / false, on / off, up / down, left / right, etc. Meanwhile, the term "toggle" refers to an assignment statement which alternately stores either of these two values into the flag variable in question. As an example, suppose you had written a program which generates various sounds, but you wish to be able to turn the sounds on and off at will. In this case, you may choose to create a variable called "Snd", and give it a value of either 1 ( on ) or 0 ( off ). To do so, you would first declare your Snd variable, if necessary, and assign it an initial value, with code similar to the following ( in BASIC ): Snd = 1 Now, in order to change it's value, you would use an assignment statement ( or "toggle" ) such as; Snd = 1 - Snd ( In some languages you have boolean variables with an explicit logical not operation that's perfect for toggling. In C++, bool snd = true; snd = !snd;. You can also use an exclusive or for the same effect: bool snd = true; snd ^= 1;. ) Here, we can see that, if Snd is intially set to 1, then this statement will subtract that value, leaving 0, and then store this new value back into the the variable Snd. If, on the other hand, Snd starts out set to 0, then this value is subtracted from 1, leaving 1, which is then stored into the Snd variable. In other words, each time this code is executed, the value of the Snd variable "toggles" between 0 and 1. So, to put these pieces together, one might use the following code; If ucase$(inkey$) = "S" then Snd = 1 - Snd ' if "S" key is pressed, execute toggle statement if Snd = 1 then sound 440, 1 ' if Snd variable = 1, then play sound To implement the preceding code, one would simply place the first statement somewhere in the main program loop, and then check for a Snd value of 1 before playing any sounds. Once this is done, pressing the "S" key will alternately turn all sounds on or off. While the typical values for a flag are 1 and 0, any two values will work, so long as there are never more than two specific values. For example, the statement; Z = 5 - Z toggles between two different values, depending on the starting value of Z. Assuming Z starts as either 0 or 5, then this statement will toggle the flag between these two values. If, on the other hand, Z starts as either 1 or 4, then these values will be used, while if Z is initially 2 or 3, then these values will be used, etc.. A general toggle equation would be Z = (sum of two Z values) - Z. Another common type of toggle is as follows; Var = Var * -1. In this case, a numeric variable will be toggled between corresponding positve and negative values, since any number multiplied by negative 1 produces it's own complement. ( +17 / -17, +234 / -234, etc...) This type of statement is handy for "bouncing" an animated image around the screen, a technique which is also known as "bounds testing". In this case, one might create a variable called HorizDirection ( or something similar ), and assign it a positive 1 to represent movement to the right or a negative 1 for movement to the left. ( Naturally, the same technique would be applied to vertical movement, as well ) Then, each time an edge of the screen is encountered, the appropriate toggle statement would toggle the associated variable, sending the image away in the opposite direction. Yet another type of toggle, used to modify text, or "string" variables, can be implemented using an IF / THEN / ELSE structure, as follows; IF Door$ = "Open" THEN Door$ = "Closed" ELSE Door$ = "Open" END IF